package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Guide;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.GuideService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午6:18:20 
 * 
 */
@Service
@Transactional
public class GuideServiceImpl implements GuideService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Guide guide) {
		guide.setObjectId(IdUtils.uuid());
		guide.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		guide.setUpdateAt(guide.getCreatedAt());
		factory.getCurrentSession().persist(guide);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Guide where village = :village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Guide where village = :village order by updateAt desc");
		query.setParameter("village", village);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void update(Guide guide) {
		String hql = "update Guide set subject =:subject, content =:content, updateAt =:updateAt where objectId =:id";
		Query query =factory.getCurrentSession().createQuery(hql);
		query.setParameter("subject", guide.getSubject());
		query.setParameter("content", guide.getContent());
		query.setParameter("updateAt", String.valueOf(System.currentTimeMillis()));
		query.setParameter("id", guide.getObjectId());
		query.executeUpdate();
	}

	@Override
	public void delete(Guide guide) {
		factory.getCurrentSession().delete(guide);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void query(Village village, String key, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Guide where village =:village and (subject like :key or content like :key)");
		query.setParameter("village", village);
		query.setParameter("key", "%"+key+"%");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Guide where village =:village and (subject like :key or content like :key) order by updateAt desc");
		query.setParameter("village", village);
		query.setParameter("key", "%"+key+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Guide get(String objectId) {
		String hql = "from Guide where objectId =:id";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("id", objectId);
		return (Guide) query.uniqueResult();
	}

}
